package com.jeeplus.modules.monitor.task;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.flowable.CheckCycle.Service.BasicdataCheckStandardMainCycleService;
import com.jeeplus.modules.flowable.CheckCycle.entity.BasicdataCheckStandardMainCycle;
import com.jeeplus.modules.flowable.CheckCycle.web.MyCheckCycleController;
import com.jeeplus.modules.flowable.checkPlanTable.entity.CheckPlanTable;
import com.jeeplus.modules.flowable.checkPlanTable.service.CheckPlanTableService;
import com.jeeplus.modules.flowable.service.FlowProcessService;
import com.jeeplus.modules.flowable.service.FlowTaskService;
import com.jeeplus.modules.flowable.web.FlowableProcessController;
import com.jeeplus.modules.flowable.web.FlowableTaskController;
import com.jeeplus.modules.monitor.CheckCycleStart.entity.CheckPlanTableJob;
import com.jeeplus.modules.monitor.CheckCycleStart.service.CheckCycleStartService;
import com.jeeplus.modules.monitor.entity.Task;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableObjectNotFoundException;
import org.flowable.engine.TaskService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class checkPlanTableTest extends Task {
    @Autowired
    CheckCycleStartService checkCycleStartService;

    @Autowired
    BasicdataCheckStandardMainCycleService basicdataCheckStandardMainCycleService;

    @Autowired
    FlowTaskService flowTaskService;

    @Autowired
    TaskService taskService;

    @Autowired
    CheckPlanTableService checkPlanTableService;

    @Override
    public void run() {
        System.out.println("这是测试任务TestTask。");
        List<CheckPlanTableJob> checkPlanTableJobs = checkCycleStartService.findcheckPlanTableJoinsList();
        for (CheckPlanTableJob a: checkPlanTableJobs) {
            SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss" );
            Date date = new Date();
            Date date1 = null;
            try {
                date1 = sdf.parse(a.getStartTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(date.getMonth() == date1.getMonth() && date.getDate() == date1.getDate()){
                MyCheckCycleController myCheckCycleController = new MyCheckCycleController();
                CheckPlanTableJob checkPlanTableJob = a;
                try {
                    BasicdataCheckStandardMainCycle basicdataCheckStandardMainCycle = basicdataCheckStandardMainCycleService.getid(a.getBasicdataCheckId());
                    String[] split = basicdataCheckStandardMainCycle.getBasicdataCheckJob().split(",");
                    String procInsId = "";
                    //指定下一步处理人
                    for (int i = 0; i < split.length; i++) {
                        if(split[i].equals("1")){
                            String saveID = basicdataCheckStandardMainCycleService.saveID(basicdataCheckStandardMainCycle);
                            FlowableTaskController flowableTaskController = new FlowableTaskController();
                            procInsId = flowTaskService.startProcess("Process_1604919255612", "check_standard_cycle", saveID, "周期点检");
                            if(StringUtils.isNotBlank(basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId())){
                                org.flowable.task.api.Task task = taskService.createTaskQuery().processInstanceId(procInsId).active().singleResult();
                                if(task != null){
                                    taskService.setAssignee(task.getId(), basicdataCheckStandardMainCycle.getBasicdataCheckCheckPeople().getId());
                                }
                            }
                        }
                    }
                    a.setCheckCycleId(procInsId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                a.setMark("已启动");
                checkCycleStartService.updataCheckPlanTable(a);
                System.out.println("已启动时间比对成功");
            }
            Date date2 = a.getEndTime();
            if(date.getMonth() == date2.getMonth() && date.getDate() == date2.getDate()){
                try {
                    a.setMark("已过期");
                    if(a.getCheckCycleId() != null){
                        FlowableProcessController flowableProcessController = new FlowableProcessController();
                        flowableProcessController.deleteProcInsCycle(a.getBasicdataCheckId(),"已过期");
                    }
                    checkCycleStartService.updataCheckPlanTable(a);
                } catch (FlowableObjectNotFoundException e) {

                }
                System.out.println("结束时间比对成功");
            }
        }
        System.out.println("执行结束");
    }
}
